#include <QDebug>
#include <QPushButton>
#include <QFile>
#include <QStringBuilder>
#include "CreateFile.h"
#include "ui_CreateFile.h"

CreateFile::CreateFile(const QString& path, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CreateFile),
    _path(path)
{
    ui->setupUi(this);
    ui->edtFileName->setBasePath(path);
    ui->edtFileName->setExtension("py");
    ui->lblPath->setText(_path);
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    connect(ui->edtFileName, SIGNAL(textChanged(QString)), SLOT(onTextChanged(QString)));
    connect(ui->buttonBox, SIGNAL(accepted()), SLOT(onAccepted()));
}

CreateFile::~CreateFile()
{
    delete ui;
}

void CreateFile::onTextChanged(const QString &)
{
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!ui->edtFileName->exists());
}

void CreateFile::onAccepted()
{
    QFile file(ui->edtFileName->fullFileName());
    if (file.open(QIODevice::WriteOnly)){
        file.close();
    }
    accept();
}
